package com.xindaoapp.happypet.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.RecommendInfo;

public class InputRegular {
    private final Context mContext;

    public InputRegular(Context context) {
        this.mContext = context;
    }

    public SpannableStringBuilder parsePraise(final List<RecommendInfo> names,
            SpannableStringBuilder style, final IRequest<RecommendInfo> iRequest) {
        int end = 0;
        int start = 0;
        RecommendSpan rs = new RecommendSpan(false, new IRequest<Boolean>() {

            @Override
            public void request(Boolean t) {
                iRequest.request(null);
            }
        });

        for (int i = 0; i < names.size(); i++) {
            if (i == 2) {
                break;
            }
            // 处理名字
            final int temp = i;
            RecommendSpan myURLSpan = new RecommendSpan(true, new IRequest<Boolean>() {

                @Override
                public void request(Boolean t) {
                    iRequest.request(names.get(temp));
                }
            });

            start = String.valueOf(style).indexOf(names.get(i).username, end);

            if (start > end) {// 如果是非关键区域，则回调false
                style.setSpan(rs, end, start, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            end = start + names.get(i).username.length();
            style.setSpan(myURLSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        if (start < style.length()) {
            style.setSpan(rs, end, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        return style;
    }

    class RecommendSpan extends ClickableSpan {
        private final IRequest<Boolean> iRequest;
        private final boolean isUsedPart;

        public RecommendSpan(boolean isUsedPart, IRequest<Boolean> iRequest) {
            this.iRequest = iRequest;
            this.isUsedPart = isUsedPart;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            if (isUsedPart) {
                ds.setColor(mContext.getResources().getColor(R.color.post_detail_praised_selected));
            } else {
                ds.setColor(mContext.getResources().getColor(R.color.post_detail_praised_normal));
            }
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(View widget) {
            iRequest.request(true);
        }
    }

    public class InputTypeCall {
        public INPUTTYPE type;
        public String name;

        public InputTypeCall(INPUTTYPE type, String name) {
            super();
            this.type = type;
            this.name = name;
        }
    }

    /**
     * 解析评论内容
     * 
     * @param name
     * @param style
     * @param iRequest
     * @return
     */
    public SpannableStringBuilder parseComment(String name, String time,
            SpannableStringBuilder style, final IRequest<Boolean> iRequest) {
        // 处理名字
        NameSpan myURLSpan =
                new NameSpan(
                        mContext.getResources().getDimensionPixelSize(R.dimen.text_size_small),
                        mContext.getResources().getColor(R.color.post_detail_blue),
                        new IRequest<Boolean>() {

                            @Override
                            public void request(Boolean t) {
                                iRequest.request(true);
                            }
                        });
        style.setSpan(myURLSpan, 0, name.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        int timeStart = String.valueOf(style).lastIndexOf(time);
        // style.setSpan(span, name.length() + 1, timeStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        style.setSpan(
                new NameSpan(mContext.getResources()
                        .getDimensionPixelSize(R.dimen.text_size_xsmall), mContext.getResources()
                        .getColor(R.color.post_detail_time), new IRequest<Boolean>() {

                    @Override
                    public void request(Boolean t) {
                        iRequest.request(false);
                    }
                }), timeStart, style.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // return CommonUtil.convetToHtml(CommonUtil.parseEmoji(style.toString()), mContext);
        return style;
    }

    class NameSpan extends ClickableSpan {
        private final IRequest<Boolean> iRequest;
        private final int textColor;
        private final int textSize;

        public NameSpan(int textSize, int textColor, IRequest<Boolean> iRequest) {
            this.textSize = textSize;
            this.textColor = textColor;
            this.iRequest = iRequest;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(textColor);
            ds.setTextSize(textSize);
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(View widget) {
            iRequest.request(true);
        }
    }
    public enum INPUTTYPE {
        URL, AITE, NAME
    }

    public SpannableStringBuilder parse(SpannableStringBuilder style,
            IRequest<InputTypeCall> iRequest) {
        // parse(style, match(aiteReg, style), style, INPUTTYPE.AITE, iRequest);
        parse(style, match(urlReg, style), style, INPUTTYPE.URL, iRequest);
        return style;
    }

    private List<String> match(String pattern, CharSequence input) {
        List<String> list = new ArrayList<String>();
        // Matcher mat = Pattern.compile(pattern).matcher(input);
        Matcher mat2 = Pattern.compile(aiteReg).matcher(input);
        // while (mat.find()) {
        // list.add(mat.group());
        // }
        while (mat2.find()) {
            if (!list.contains(mat2.group())) {
                list.add(mat2.group());
            }
        }
        return list;
    }

    private SpannableStringBuilder parse(SpannableStringBuilder style, List<String> list,
            CharSequence input, final INPUTTYPE inputType, final IRequest<InputTypeCall> iRequest) {
        int end = 0;
        for (String str : list) {
            final String temp = str;
            MyURLSpan myURLSpan = new MyURLSpan(str, inputType, new IRequest<InputTypeCall>() {

                @Override
                public void request(InputTypeCall t) {
                    iRequest.request(new InputTypeCall(inputType, temp));
                }
            });
            int start = String.valueOf(input).indexOf(str, end);
            end = start + str.length();
            style.setSpan(myURLSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        // SpannableStringBuilder ssb =
        // CommonUtil.convetToHtml(
        // CommonUtil.parseEmoji(style.toString().replaceAll(aiteReg, "网页链接")),
        // mContext);
        // style = ssb;
        return style;
    }

    private static final String aiteReg =
            "((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    // "http(s)?://([a-zA-Z|\\d]+\\.)+[a-zA-Z|\\d]+(/[a-zA-Z|\\d|\\-|\\+|_./?%=]*)?";
    private static final String urlReg = "http://.*?\\.html";


    class MyURLSpan extends ClickableSpan {
        private final String url;
        private final INPUTTYPE inputType;
        private final IRequest<InputTypeCall> iRequest;

        public MyURLSpan(String url, INPUTTYPE inputType, IRequest<InputTypeCall> iRequest) {
            this.url = url;
            this.inputType = inputType;
            this.iRequest = iRequest;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ds.linkColor);
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(View widget) {
            iRequest.request(new InputTypeCall(inputType, url));
        }
    }
}
